/**
 * 将该脚本挂载到Canvas节点上,设置好用于放大缩小的摄像机,最大比例,以及初始状态即可
 * web端调试 w放大s缩小
 */
const Features  = cc.Enum({
    '缩放' : 1,
    '平移' : 2,
    '平移和缩放' : 3,
    '全部关闭' : 4
})
const PointType = cc.Enum({
    '平移模式': 1,
    '缩放模式': 2,
    '全部关闭': 3
})
const RectType = cc.Enum({
    '关闭': 1,
    '手动模式': 2,
    '节点模式': 3           //这个暂时没用
})

// class RenderRange


const { ccclass, property } = cc._decorator;
@ccclass
export default class ScaleClass extends cc.Component {

    @property({
        type: cc.Node,
        displayName: '摄像机',
        tooltip: '缩放放大用的摄像机'
    })
    mainCamera: cc.Node = null;



    @property({
        displayName: '最大比例',
        tooltip: '在起始缩放比上做乘法'
    })
    maxScale = 2;
    @property({
        displayName: '最小比例',
        tooltip: '在起始缩放比上做乘法',
        min : 0
    })
    minScale = 1;

    //触发选择事件
    @property({
        type: cc.Component.EventHandler,
        displayName : '缩放触发的事件',
        tooltip: CC_DEV && '缩放触发事件',
    })
    selectedEvent: cc.Component.EventHandler = null;

    @property({
        displayName: '摄像机默认比例',
        tooltip: '摄像机默认的比例zoomRatio',
    })
    designScale : number = 1;

    @property
    _zoomScale: number = 1;
    @property({
        displayName: '摄像机比例',
        tooltip: '摄像机默认的比例zoomRatio',
        visible:false
    })
    set zoomScale(value) {
        let v = value.toFixed(1);
        value = parseFloat(v);
        if (value > this.maxScale) value = this.maxScale;
        if (value < this.minScale) value = this.minScale;
        let lastValue = this._zoomScale;
        this._zoomScale = value;
        if (this.selectedEvent) {
            this.selectedEvent.emit([this._zoomScale]);
        }
        if (this.bottomSwitch && this._zoomScale <= 1){
            let height = cc.winSize.height/2 * (1-this._zoomScale)/this._zoomScale;
            this.mainCamera.y = height;
        }
        this.correctPos();
    }
    get zoomScale() {
        return this._zoomScale;
    }

    @property({
        type : Features,
        displayName : '总开关',
        tooltip : '控制功能开关,平移需要摄像机缩放倍数大于1'
    })
    mainSwitch = Features.平移和缩放;

    @property({
        displayName:'底部模式',
        tooltip: "摄像成像靠底部"
    })
    bottomSwitch = false;
    
    @property
    _renderNode : cc.Node = null;
    @property({
        type :cc.Node,
        visible () {return this.bottomSwitch},
        displayName : "渲染节点",
        tooltip : '需要一直靠在底部的节点'
    })
    set renderNode (node){
        this._renderNode = node;
    };
    get renderNode (){
        return this._renderNode;
    }

    @property({
        displayName:'摄像尺寸模式',
        tooltip: "1.不使用 2.手动输入区域宽高 3.摄像某一个节点",
        type : RectType
    })
    rdType = RectType.关闭;     //节点模式暂时没用
    @property({
        // type : cc.Size,
        visible () {return this.rdType == RectType.手动模式},
        displayName : "摄像的尺寸大小",
        tooltip : '输入可视化区域的宽高'
    })
    rdRange : cc.Size = cc.size(0,0);
    @property({
        type :cc.Node,
        visible () {return this.rdType == RectType.节点模式},
        displayName : "摄像的节点",
        tooltip : '拖入摄像机摄的节点'
    })
    rdNode : cc.Node = null;



    eventArray : cc.Touch[] = [];
    firstTouchVec: cc.Vec2 = null;
    startLength: number = 0;
    bottomPad : number = 0 ;

    @property
    _eventType: number = PointType.全部关闭;
    @property({
        type : PointType,
        visible:false,
        tooltip:'配合开关使用,切勿冲突'
    })
    set eventType(value: number) {
        this._eventType = value;
        if (value == PointType.平移模式) {
            this.eventArray.length = 0;
            if (this.mainSwitch != Features.平移 && this.mainSwitch != Features.平移和缩放){
                this._eventType = PointType.全部关闭;
            }
        }
        else if (value == PointType.缩放模式) {
            if (this.mainSwitch != Features.缩放 && this.mainSwitch != Features.平移和缩放){
                this._eventType = PointType.全部关闭;
            }
        }
        else if (value == PointType.全部关闭) {
            this.eventArray.length = 0;
            this.firstTouchVec = null;
            this.startLength = null;
        }
    }
    get eventType() {
        return this._eventType;
    }

    onLoad() {
        let self = this;
        self.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);

        // cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP);
        // cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,  (event)=> {
        //     if (event.keyCode == cc.macro.KEY.w) {
        //         self.zoomScale += 0.1;
        //     }
        //     else if (event.keyCode == cc.macro.KEY.s){
        //         self.zoomScale -= 0.1;
        //     }
        //     else if (event.keyCode == cc.macro.KEY.d){
        //         self.mainCamera.y -= 5;
        //     }
        //     else if (event.keyCode == cc.macro.KEY.f){
        //         self.mainCamera.y += 5;
        //     }
        //     self.mainCamera.getComponent(cc.Camera).zoomRatio = self.zoomScale * self.designScale;
        // })
        if (this.bottomSwitch){

            let height = this.renderNode.height * this.renderNode.scaleY;
            let y = this.renderNode.y - height/2;
            let pad = -cc.winSize.height/2 - y;
            this.bottomPad = pad;
        }

        this.node.on('recovery',this.recovery,this);
    }
    //rectSize : cc.size(width,height)
    /**
     * 
     * @param defaultScale 默认缩放比例,一般是0.5或者1
     * @param mainSwitch 缩放:1,平移:2,缩+平:3,关闭:4
     * @param rectSize 
     */
    recovery(defaultScale,mainSwitch = Features.平移和缩放,rectSize : cc.Size = null){
        Log('新设置默认参数:',defaultScale,mainSwitch)
        this.designScale = defaultScale;
        this.mainCamera.getComponent(cc.Camera).zoomRatio = defaultScale;
        this.zoomScale = 1;
        this.mainCamera.position = cc.Vec2.ZERO;

        this.mainSwitch = mainSwitch;

        if (rectSize != null){
            this.rdType = RectType.手动模式;
            this.rdRange = rectSize;
        }
        else{
            this.rdType = RectType.关闭;
        }
    }
    getDelta() {
        if (this.eventArray.length < 2) {
            return null;
        }
        let pos1 = this.eventArray[0].getLocation();
        let pos2 = this.eventArray[1].getLocation();
        let currLength = pos1.sub(pos2).mag();
        return currLength;
    }
    touchStart(event: cc.Event.EventTouch) {
        let array: cc.Touch[] = event.getTouches();
        let count = array.length;
        if (count == 1) {
            //单点触摸
            if (this.eventArray.indexOf(event.touch) < 0) {
                if (this.eventArray.length == 0) {
                    this.eventType = PointType.平移模式;
                    this.eventArray.push(event.touch);
                    this.firstTouchVec = event.getLocation();
                }
                else if (this.eventArray.length == 1) {
                    this.eventType = PointType.缩放模式;
                    this.eventArray.push(event.touch);
                    this.startLength = this.getDelta();
                }
                else if (this.eventArray.length >= 2){
                    this.eventType = PointType.全部关闭;
                }
            }
        }
        else if (count == 2) {
            this.eventType = PointType.缩放模式;
            this.eventArray.concat(array);
            //多点触摸
            this.startLength = this.getDelta();
        }
        else if (count > 2){
            this.eventType = PointType.全部关闭;
        }
    }
    touchMove(event: cc.Event.EventTouch) {
        let count = event.getTouches().length;
        if (this.eventType == PointType.平移模式 && (this.mainSwitch == Features.平移 || this.mainSwitch == Features.平移和缩放)) {
            let vec = event.getDelta();
            let zoomScale = this.mainCamera.getComponent(cc.Camera).zoomRatio;
            vec.mulSelf(zoomScale);
            if (this.mainCamera.getComponent(cc.Camera).zoomRatio>=this.designScale){
                this.mainCamera.position = this.mainCamera.position.sub(vec);
                this.correctPos();
            }
        }
        else if (count == 2 && (this.mainSwitch == Features.缩放 || this.mainSwitch == Features.平移和缩放)) {
            let currLength = this.getDelta();
            if (currLength!=null){
                let scale = (currLength - this.startLength) /1000;
                this.zoomScale *= (scale+1);
                let realScale = this.zoomScale * this.designScale;
                this.mainCamera.getComponent(cc.Camera).zoomRatio = realScale;
            }
        }

    }
    touchEnd(event) {
        this.eventType = PointType.全部关闭;
    }
    correctPos() {
        //暂时不修正坐标了 让他自己跑
        return;
        var self = this;
        let zoomScale = this.mainCamera.getComponent(cc.Camera).zoomRatio;
        let leftLimit = -cc.winSize.width/2,rightLimit = cc.winSize.width/2,
            topLimit = cc.winSize.height/2,downLimit = -cc.winSize.height/2;
        
        let rdSize = cc.size(0,0);
        switch(this.rdType){
            case RectType.关闭:
                rdSize = cc.size(cc.winSize.width,cc.winSize.height);
                break;
            case RectType.手动模式:
                rdSize = cc.size(this.rdRange.width,this.rdRange.height);
                break;
            case RectType.节点模式:
                rdSize = cc.size(this.rdNode.width,this.rdNode.height);
                break;
        }
        rdSize.width = rdSize.width * zoomScale;
        rdSize.height = rdSize.height * zoomScale;
        // let width = Math.abs((cc.winSize.width / 2) * (zoomScale - this.designScale) / zoomScale);
        // let height = Math.abs((cc.winSize.height / 2) * (zoomScale - this.designScale) / zoomScale);
        if (self.mainCamera.x + rdSize.width/2 <= rightLimit) {
            self.mainCamera.x = rightLimit-rdSize.width/2;
        }
        if (self.mainCamera.x - rdSize.width/2 >= leftLimit) {
            self.mainCamera.x = leftLimit + rdSize.width/2;
        }
        if (self.mainCamera.y - rdSize.height/2 >= downLimit) {
            self.mainCamera.y = downLimit + rdSize.height/2;
        }
        if (self.mainCamera.y + rdSize.height/2 <= topLimit) {
            self.mainCamera.y = topLimit - rdSize.height/2;
        }
    }
}
